package com.bifang.module.file.common.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/** 文件比对工具 */
public class FileDiffUtil {
    /**
     * 判断两个文件是否相同
     *
     * @param file 文件
     * @param md5 老的md5值
     * @return
     */
    public static boolean check(File file, String md5) {
        String newMd5 = getMD5(file);
        return checkMd5(newMd5, md5);
    }

    /**
     * 判断两个文件是否相同
     *
     * @param newFile 新的 文件
     * @param oldFile 老的 文件
     * @return
     */
    public static boolean check(File newFile, File oldFile) {

        String newMd5 = getMD5(newFile);
        String oldMd5 = getMD5(oldFile);
        return checkMd5(newMd5, oldMd5);
    }

    /**
     * 判断两个文件是否相同
     *
     * @param newPath 新的 文件路径
     * @param oldPath 老的 文件路径
     * @return
     */
    public static boolean checkPath(String newPath, String oldPath) {

        String newMd5 = getMD5(newPath);
        String oldMd5 = getMD5(oldPath);
        return checkMd5(newMd5, oldMd5);
    }

    /**
     * 判断两个文件是否相同
     *
     * @param path 文件 路径
     * @param md5 老的md5值
     * @return
     */
    public static boolean check(String path, String md5) {
        String newMd5 = getMD5(path);
        return checkMd5(newMd5, md5);
    }

    /** 交易两个MD5值是否相同 */
    public static boolean checkMd5(String newMd5, String oldMd5) {
        boolean isSame = false;
        if (newMd5.equals(oldMd5)) {
            isSame = true;
        } else {
            isSame = false;
        }
        return isSame;
    }

    public static String getMD5(MultipartFile file) {
        BigInteger bi = null;
        InputStream fis = null;
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = file.getInputStream();
            while ((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            fis.close();
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (bi.toString(16).length() != 32) { // 避免BigInteger转16进制首位0丢失情况

            return 0 + bi.toString(16);
        }
        return bi.toString(16);
    }

    public static String getMD5(byte[] file) {
        BigInteger bi = null;
        try {
            int len = getValidLength(file);
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(file, 0, len);
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (bi.toString(16).length() != 32) { // 避免BigInteger转16进制首位0丢失情况
            return 0 + bi.toString(16);
        }
        return bi.toString(16);
    }

    public static int getValidLength(byte[] bytes) {
        int i = 0;
        if (null == bytes || 0 == bytes.length) {
            return i;
        }
        return bytes.length;
    }

    public static String getMD5(InputStream fis) {
        BigInteger bi = null;
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            while ((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (bi.toString(16).length() != 32) { // 避免BigInteger转16进制首位0丢失情况

            return 0 + bi.toString(16);
        }
        return bi.toString(16);
    }

    public static String getMD5(File file) {
        BigInteger bi = null;
        FileInputStream fis = null;
        try {
            byte[] buffer = new byte[1024];
            int len = 0;
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);
            while ((len = fis.read(buffer)) != -1) {
                md.update(buffer, 0, len);
            }
            fis.close();
            byte[] b = md.digest();
            bi = new BigInteger(1, b);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (bi.toString(16).length() != 32) { // 避免BigInteger转16进制首位0丢失情况

            return 0 + bi.toString(16);
        }
        return bi.toString(16);
    }

    public static String getMD5(String path) {
        File f = new File(path);
        return getMD5(f);
    }

    public static void main(String[] args) {
        // System.out.println(FileDiffUtil.check("G:/upload/564d9fdae51e55fd7d179ad9aed0cd93ef57a6011d865cab67291a33f034a443.png", "bb318d4b55126a648fe1e06d31e70e0a"));
        // System.out.println(FileDiffUtil.checkPath("G:/upload/564d9fdae51e55fd7d179ad9aed0cd93ef57a6011d865cab67291a33f034a443.png", "G:/upload/其他-BB318D4B55126A648FE1E06D31E70E0A.png-123-20190824175121-12137.png"));
        String s = "abcdefg.jpg";
        System.out.println(s.concat("fg").concat(".jpg"));
    }
}
